from selenium import webdriver
import unittest

from selenium.webdriver import Keys
from selenium.webdriver.common.by import By


class NewVisitorTest(unittest.TestCase):
    def setUp(self):
        self.browser = webdriver.Chrome()

    def tearDown(self):
        self.browser.quit()

    def check_for_row_in_list_table(self,row_text):
        table = self.browser.find_element(by=By.ID, value='id_list_table')
        rows = table.find_elements(by=By.TAG_NAME, value='tr')
        self.assertIn(row_text, [row.text for row in rows])

    def send_text_input_box(self,input_text):
        input_box = self.browser.find_element(by=By.ID, value='id_new_item')
        self.assertEqual(
            input_box.get_attribute('placeholder'),
            'Enter a to-do item'
        )
        input_box.send_keys(input_text)
        input_box.send_keys(Keys.ENTER)


    def test_can_start_a_list_and_retrieve_it_later(self):
        self.browser.get("http://localhost:8000")
        self.assertIn('To-Do', self.browser.title)
        print(self.browser.title)

        # header_text=self.browser.find_element_by_tag_name('h1').text
        header_text = self.browser.find_element(by=By.TAG_NAME, value='h1').text
        self.assertIn('To-Do', header_text)

        self.send_text_input_box("Buy peacock feathers")
        import time
        time.sleep(10)
        self.check_for_row_in_list_table('1: Buy peacock feathers')

        self.send_text_input_box("Use peacock feathers to make a fly")
        time.sleep(10)
        self.check_for_row_in_list_table('1: Buy peacock feathers')
        self.check_for_row_in_list_table('2： Use peacock feathers to make a fly')

        self.fail('Finish the test!')


if __name__ == '__main__':
    unittest.main()
